﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace ResxLocalizationEditor {
	public partial class AddLanguageEditor : Form {
		public AddLanguageEditor() {
			InitializeComponent();
		}

		CultureInfo _ret = null;

        public CultureInfo SelectedCultureCode {
			get {
				if (_ret == null)
					throw new InvalidOperationException();
				return _ret;
			}
            private set {
                if (value == null)
                    throw new ArgumentNullException();
                _ret = value;
            }
		}

		private void ok_click(object sender, EventArgs e) {
			try {
				//Validity check
				var culture = new CultureInfo(txtLangCode.Text);
                SelectedCultureCode = culture;

				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			catch {
				MessageBox.Show("Language code '" + txtLangCode.Text + "' is not valid.");
			}
		}

		private void langCode_change(object sender, EventArgs e) {
			try {
				var culture = new CultureInfo(txtLangCode.Text);

				txtLocale.Text = culture.EnglishName;
			}
			catch {
				txtLocale.Text = "Not valid";
			}
		}

		private void LangCode_keyUp(object sender, KeyEventArgs e) {
			if (e.KeyCode == Keys.Enter) {
				ok_click(this, e);
			}
		}
	}
}
